package com.example.anydo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.anydo.R;
import com.example.anydo.bean.LoginBackBean;
import com.example.anydo.bean.SynchronizeBodyBean;
import com.example.anydo.constant.Url;
import com.example.anydo.net.DataBack;
import com.example.anydo.net.NetClient;
import com.example.anydo.util.database.DatabaseHelper;
import com.example.anydo.util.pub_util.SPUtil;
import com.example.anydo.util.pub_util.Synchronizer;
import com.example.anydo.util.pub_util.Util;

public class LoginActivity extends AppCompatActivity {

    private TextView register;
    private EditText userName;
    private EditText psw;
    private Button login;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        boolean hasLogin = (boolean) SPUtil.getData(this,SPUtil.KEY_LOGIN,false);
        if(hasLogin){
            jumpToMain();
        }

        userName=(EditText)findViewById(R.id.userName);
        psw=(EditText)findViewById(R.id.psw);
        login=(Button)findViewById(R.id.login);
        register=(TextView)findViewById(R.id.register);


        login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String name=userName.getText().toString();
                String password=psw.getText().toString();

                if(checkInfo(name,password)){
                    startLogin(name,password);
                }
            }
        });

        register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent i = new Intent(LoginActivity.this , RegisterActivity.class);
                startActivity(i);
            }
        });

    }

    private void jumpToMain() {
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void loginSuccess(final String id){
        final DatabaseHelper dbHelper = new DatabaseHelper(this);
        String name = dbHelper.getUserNameById(Integer.parseInt(id));

        Log.e("UserId", "put: " + id );
        SPUtil.putUserId(LoginActivity.this, id);

        if(name.equals("")){  // 第一次使用该账号
            Synchronizer synchronizer = new Synchronizer(this);
            synchronizer.downloadData(id,new DataBack<SynchronizeBodyBean>() {
                @Override
                public void onResponse(SynchronizeBodyBean data) {
                    if(data.getCode() == 200){
                        if(dbHelper.addUserCache(data)){

                            SPUtil.putData(LoginActivity.this,SPUtil.KEY_LOGIN,true);
                            jumpToMain();
                            Util.toast(LoginActivity.this,data.getMessage());
                        }else {
                            Util.toast(LoginActivity.this,"缓存数据失败！");
                        }
                    }else {
                        Util.toast(LoginActivity.this,data.getMessage());
                    }
                }
                @Override
                public void onFailure(String msg) {
                    Util.toast(LoginActivity.this,"出了一点点问题...");
                }
            });
        }else {
            SPUtil.putData(LoginActivity.this,SPUtil.KEY_LOGIN,true);
            jumpToMain();
        }
    }


    private void startLogin(String name, String password) {
        NetClient.getInstance()
                .withUrl(Url.BASE_URL + Url.LOGIN_URL)
                .activity(this)
                .setPlayer("登录中")
                .addParams("name",name)
                .addParams("password",password)
                .asObject(LoginBackBean.class)
                .doGet(new DataBack<LoginBackBean>() {
                    @Override
                    public void onResponse(LoginBackBean data) {
                        if(data.getCode() == 200){ // 登录成功
                            Util.toast(LoginActivity.this,data.getMessage());

                            loginSuccess(data.getData().getId()+"");


                        }else {  // 登录失败
                            Util.toast(LoginActivity.this,data.getMessage());
                        }
                    }
                    @Override
                    public void onFailure(String msg) {
                        Util.toast(LoginActivity.this,"出了一点点问题...");
                    }
                });
    }

    private boolean checkInfo(String name, String password) {
        if(name.equals("")){
            Util.toast(this,"请输入账号");
            return false;
        }else if(password.equals("")){
            Util.toast(this,"请输入密码");
            return false;
        }else if(name.length()<6){
            Util.toast(this,"账号不正确");
            return false;
        }else if(password.length()<6){
            Util.toast(this,"密码不正确");
            return false;
        }
        return true;
    }
}